package server;

import common.GameState;
import common.Level;
import common.Player;
import common.PlayerDirection;

public class GameStateFinished extends GameControllerState {

	
	public static final long GAME_FINISHED_TIMEOUT = 3000;
	
	
	
	@Override
	public void initializeState(GameController c) {
		super.initializeState(c);
		
		controller.setTimer(GAME_FINISHED_TIMEOUT);
		game.setPlayingState(GameState.Finished);
		for( Player p : c.getGame().getPlayer()){
			game.setMessage( p.GetName() + " wins with " + p.GetScore() + " points");
		}
	}
	
	
	@Override
	public void detachState() {
		controller.stopTimer();
	}
	
	
	@Override
	public void timerTick(int tick) {

		if (tick < 2) {
			// time for client to display game results -> score each player
		} else if (tick < 3) {

			// time to inform client about coming level change
 
		} else {
			
			// change level
			// if no more level left, controller.stopTimer() -> game end reached
			int nextLevel = level.getLevelNr()+1;
			if( nextLevel == Level.LEVELAMOUNT){
				controller.stopTimer();
			}
			//set level to...
			//new Level( level.getLevelNr()+1);
			
			// reset game state 
			//   -> player positions
			//   -> player sore
			UpdateablePlayer[] p = game.getUpdateablePlayer();
			for (int i = 0; i < p.length; i++) {
				p[i].SetDirection(PlayerDirection.NoDirection);
				int[] pos = level.getStartPosition(i);
				p[i].SetPosition(pos[0], pos[1]);
				p[i].SetScore(0);
			}
			

			// transition to playing state
			controller.setState(new GameStatePlaying());
		}
	}
	

	
}
